

package com.maoshi.shop.distribution.comment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.distribution.comment.dao.DistributionUserWalletBillMapper;
import com.maoshi.shop.distribution.comment.dao.DistributionUserWalletMapper;
import com.maoshi.shop.distribution.comment.dto.DistributionUserWalletDto;
import com.maoshi.shop.distribution.comment.model.DistributionUserWallet;
import com.maoshi.shop.distribution.comment.model.DistributionUserWalletBill;
import com.maoshi.shop.distribution.comment.service.DistributionUserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author lgh on 2019/04/01.
 */
@Service
public class DistributionUserWalletServiceImpl extends ServiceImpl<DistributionUserWalletMapper, DistributionUserWallet> implements DistributionUserWalletService {

    @Autowired
    private DistributionUserWalletMapper distributionUserWalletMapper;

    @Autowired
    private DistributionUserWalletBillMapper distributionUserWalletBillMapper;

    @Override
    public Double getSettledAmountByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getSettledAmountByDistributionUserId(distributionUserId);
    }

    @Override
    public Long getWalletIdByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getWalletIdByDistributionUserId(distributionUserId);
    }

    @Override
    public Page<DistributionUserWallet> getDistributionUserWalletAndDistributionUserVOPage(Page page, String userMobile) {
        return distributionUserWalletMapper.getDistributionUserWalletAndDistributionUserVOPage(page,userMobile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDistributionUserWallet(DistributionUserWallet distributionUserWallet, Long modifier) {
        //获取到变动前的钱包
        DistributionUserWallet oldDistributionUserWallet=distributionUserWalletMapper.selectById(distributionUserWallet.getWalletId());
        //变动数额
        Double unsettledAmount = Arith.sub(distributionUserWallet.getUnsettledAmount(), oldDistributionUserWallet.getUnsettledAmount()) ;
        Double settledAmount = Arith.sub(distributionUserWallet.getSettledAmount(), oldDistributionUserWallet.getSettledAmount());
        Double invalidAmount =Arith.sub(distributionUserWallet.getInvalidAmount(), oldDistributionUserWallet.getInvalidAmount());
        Double addupAmount = Arith.sub(distributionUserWallet.getAddupAmount(), oldDistributionUserWallet.getAddupAmount());
        //修改钱包
        distributionUserWalletMapper.updateById(distributionUserWallet);
        //增加钱包流水记录
        DistributionUserWalletBill distributionUserWalletBill = new DistributionUserWalletBill(distributionUserWallet,"人工修改",unsettledAmount,settledAmount,invalidAmount,addupAmount,1);
        distributionUserWalletBill.setModifier(modifier);
        distributionUserWalletBillMapper.insert(distributionUserWalletBill);
    }

    @Override
    public DistributionUserWalletDto getDistributionUserWalletDtoByDistributionUserId(Long distributionUserId) {
        return distributionUserWalletMapper.getDistributionUserWalletDtoByDistributionUserId(distributionUserId);
    }


}
